package moe.fuqiuluo.shamrock.remote.api

import moe.fuqiuluo.shamrock.helper.LogicException
import io.ktor.server.application.call
import io.ktor.server.response.respondText
import io.ktor.server.routing.Routing
import moe.fuqiuluo.shamrock.remote.action.ActionManager
import moe.fuqiuluo.shamrock.remote.action.ActionSession
import moe.fuqiuluo.shamrock.remote.action.handlers.*
import moe.fuqiuluo.shamrock.tools.fetchOrNull
import moe.fuqiuluo.shamrock.tools.fetchOrThrow
import moe.fuqiuluo.shamrock.tools.getOrPost

fun Routing.troopAction() {
    getOrPost("/group_touch") {
        val groupId = fetchOrThrow("group_id")
        val userId = fetchOrThrow("user_id")
        call.respondText(GroupPoke(groupId, userId))
    }

    getOrPost("/get_group_honor_info") {
        val groupId = fetchOrThrow("group_id")
        val refresh = fetchOrNull("refresh")?.toBooleanStrict() ?: false
        call.respondText(GetTroopHonor(groupId, refresh))
    }

    getOrPost("/get_group_member_list") {
        val groupId = fetchOrThrow("group_id")
        val refresh = fetchOrNull("refresh")?.toBooleanStrict() ?: false
        call.respondText(GetTroopMemberList(groupId, refresh))
    }

    getOrPost("/get_group_member_info") {
        val groupId = fetchOrThrow("group_id")
        val userId = fetchOrThrow("user_id")
        val refresh = fetchOrNull("no_cache")?.toBooleanStrict() ?: false
        call.respondText(GetTroopMemberInfo(groupId, userId, refresh))
    }

    getOrPost("/get_group_list") {
        val refresh = fetchOrNull("refresh")?.toBooleanStrict() ?: true
        call.respondText(GetTroopList(refresh))
    }

    getOrPost("/get_group_info") {
        val groupId = fetchOrThrow("group_id")
        val refresh = fetchOrNull("no_cache")?.toBooleanStrict() ?: false
        call.respondText(GetTroopInfo(groupId, refresh))
    }

    getOrPost("/set_group_special_title") {
        val groupId = fetchOrThrow("group_id")
        val userId = fetchOrThrow("user_id")
        val title = fetchOrThrow("special_title")
        call.respondText(SetGroupUnique(groupId, userId, title))
    }

    getOrPost("/set_group_name") {
        val groupId = fetchOrThrow("group_id")
        val card = fetchOrThrow("group_name")
        call.respondText(ModifyTroopName(groupId, card))
    }

    getOrPost("/set_group_card") {
        val groupId = fetchOrThrow("group_id")
        val userId = fetchOrThrow("user_id")
        val card = fetchOrNull("card") ?: ""
        call.respondText(ModifyTroopMemberName(groupId, userId, card))
    }

    getOrPost("/set_group_admin") {
        val groupId = fetchOrThrow("group_id") .toLong()
        val userId = fetchOrThrow("user_id") .toLong()
        val enable = fetchOrThrow("enable").toBooleanStrict()
        call.respondText(SetGroupAdmin(groupId, userId, enable))
    }

    getOrPost("/set_group_whole_ban") {
        val groupId = fetchOrThrow("group_id") .toLong()
        val enable = fetchOrThrow("enable").toBooleanStrict()
        call.respondText(SetGroupWholeBan(groupId, enable))
    }

    getOrPost("/set_group_ban") {
        val groupId = fetchOrThrow("group_id") .toLong()
        val userId = fetchOrThrow("user_id") .toLong()
        val duration = fetchOrNull("duration")?.toInt() ?: (30 * 60)

        call.respondText(BanTroopMember(groupId, userId, duration))
    }

    getOrPost("/set_group_kick") {
        val userId = fetchOrThrow("user_id").toLong()
        val groupId = fetchOrThrow("group_id").toLong()
        call.respondText(KickTroopMember(groupId, userId))
    }
}